package pl.edu.pb.wi.pwnography.tree;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import quickdt.Attributes;

public class Node {
    private Map<Serializable, Node> children;
    private String attribute;
    private Double entropy;
    private Serializable result;

    public Map<Serializable, Node> getChildren() {
	return children;
    }

    public void setChildren(Map<Serializable, Node> children) {
	this.children = children;
    }

    public String getAttribute() {
	return attribute;
    }

    public void setAttribute(String attribute) {
	this.attribute = attribute;
    }

    public Double getEntropy() {
	return entropy;
    }

    public void setEntropy(Double entropy) {
	this.entropy = entropy;
    }

    public Serializable getResult() {
	return result;
    }

    public void setResult(Serializable result) {
	this.result = result;
    }

    public Node() {
	setChildren(new HashMap<Serializable, Node>());
	setAttribute(null);
	setEntropy(null);
	setResult(null);
    }

    public Node classification(Attributes attributes) {
	return (getResult() != null) ? this : children.get(
		attributes.get(attribute)).classification(attributes);
    }

    @Override
    public String toString() {
	StringBuilder sb = new StringBuilder("\n[");
	if (attribute != null)
	    sb.append("Attribute=").append(attribute).append(" ");
	if (entropy != null)
	    sb.append("Entropy=").append(entropy).append(" ");
	if (result != null)
	    sb.append("Result=").append(result).append(" ");
	sb.append("]");

	return sb.toString();
    }

    public String toString(String marker) {
	marker = String.format("%s ", marker);
	StringBuilder sb = new StringBuilder(marker).append("->[");
	if (attribute != null)
	    sb.append("Attribute=").append(attribute).append(" ");
	if (entropy != null)
	    sb.append("Entropy=").append(entropy).append(" ");
	if (result != null)
	    sb.append("Result=").append(result).append(" ");
	sb.append("]\n");

	for (Map.Entry<Serializable, Node> node : children.entrySet()) {
	    sb.append(marker)
		    .append(String.format("(%s Child: %s) \n", attribute, node
			    .getKey().toString()))
		    .append(node.getValue().toString(marker));
	}

	return sb.toString();
    }

}
